package sctu.edu.java.year2016.team02.huangguojing.day20160605.Chapter12_2;

/**
 * Created by Eoly on 2016/6/5.
 * 编写程序，使用字符输入、输出流读取文件，将一段文字加密后存入文件，然后再读取，并将加密前与加密后的文件输出。
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

class fileOperate {
    String Path;
    String Name;
    File file;

    public fileOperate(File file) {
        this.file=file;
    }

    public fileOperate(String Path) {
        this.Path=Path;
        file=new File(Path);
        if(!file.exists()){
            try {
                file.createNewFile();
                System.out.println("File is Created.");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    fileOperate(String Path,String Name){
        this.Name=Name;
        this.Path=Path;
        file=new File(Path,Name);
        if(!file.exists()){
            try {
                file.createNewFile();
                System.out.println("File is Created.");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    String inPut() {
        try {
            FileInputStream in=new FileInputStream(file);
            byte[] byt= new byte[1024];
            String line=new String(byt,0,byt.length);
            in.close();
            return line;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "null";
    }
    boolean outPut(String words){
        try {
            FileOutputStream out = new FileOutputStream(file);
            byte[] byt=words.getBytes();
            out.write(byt);
            out.close();
            System.out.println("Writing successful!");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
